package com.udaye.movie.ui.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;

import com.ego.shadow.Interstitial;
import com.udaye.movie.R;
import com.udaye.movie.adapter.FragmentAdapter;
import com.udaye.movie.ui.fragment.CommingSoonFragment;
import com.udaye.movie.ui.fragment.InTheaterFragment;
import com.udaye.movie.ui.fragment.Top250Fragment;
import com.udaye.movie.ui.fragment.UsBoxFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private ViewPager mViewPager;
    private BottomNavigationView bnv_view;
    private List<Fragment> fragments = new ArrayList<>(4);
//    private List<String> titles = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initActionBar();
        bnv_view = findViewById(R.id.bnv_view);
        mViewPager = findViewById(R.id.vp_channel_pager);
        bnv_view.setOnNavigationItemSelectedListener(mBottomItemListener);

        fragments.add(new InTheaterFragment());
        fragments.add(new CommingSoonFragment());
        fragments.add(new Top250Fragment());
        fragments.add(new UsBoxFragment());

        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i){
                    case 0:
                        bnv_view.setSelectedItemId(R.id.navigation_in_theater);
                        break;
                    case 1:
                        bnv_view.setSelectedItemId(R.id.navigation_coming_soon);
                        break;
                    case 2:
                        bnv_view.setSelectedItemId(R.id.navigation_top250);
                        break;
                    case 3:
                        bnv_view.setSelectedItemId(R.id.navigation_us_box);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        Interstitial.of(this).auto();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mBottomItemListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setTitle(item.getTitle());
            }
            switch (item.getItemId()) {
                case R.id.navigation_in_theater:
                    mViewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_coming_soon:
                    mViewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_top250:
                    mViewPager.setCurrentItem(2);
                    return true;
                case R.id.navigation_us_box:
                    mViewPager.setCurrentItem(3);
                    return true;
            }

            return false;
        }
    };

    @Override
    void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
            actionBar.setTitle("正在热映");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_title_setting:
                SettingActivity.start(MainActivity.this);
                break;
        }
        return true;
    }

}
